package com.finalproject.ui;
/**
 * VendingMachine for UI
 */
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.finalproject.entity.CaloriesExceededException;
import com.finalproject.entity.CardHolder;
import com.finalproject.entity.CardHolderDatabase;
import com.finalproject.entity.CardHolderDatabase.CardHolderDatabaseObserver;
import com.finalproject.entity.InsufficientFundsException;
import com.finalproject.entity.SnackItem;
import com.finalproject.entity.VendingMachine;
import com.finalproject.entity.VendingMachine.VendingMachineException;
import com.finalproject.entity.VendingMachineDatabase;
import com.finalproject.entity.VendingMachine.VendingMachineObserver;

public class UIVendingMachine extends JPanel implements VendingMachineObserver{
	
	private VendingMachineDatabase vendingMachineDatabase = VendingMachineDatabase.getInstance();
	
	
	private  UiVBSnackItemPanel uiVBSnackItemPanel;
	
	private CardHolder cardHolder = null;
	
	private VendingMachine vendingMachine;
	
	
	
	public UIVendingMachine(VendingMachine vendingMachine) {
		setBorder(BorderFactory.createTitledBorder("VendingMachine #" + vendingMachine.getId()));
		setLayout(new BorderLayout(3,3));
		
		uiVBSnackItemPanel = new UiVBSnackItemPanel();
		uiVBSnackItemPanel.setBorder(BorderFactory.createTitledBorder("Snacks"));
		this.vendingMachine = vendingMachine;
		
		loadSnackItem(vendingMachine);
		vendingMachine.addVendingmachineObserver(this);
		
//		CardHolderDatabase.getInstance().addCardHolderDatabaseObserver(this);
		add(uiVBSnackItemPanel,BorderLayout.CENTER);
	
		
		  setPreferredSize(new Dimension(600,700));

	}
	
	public void setCardHolder(CardHolder cardHolder) {
		this.cardHolder = cardHolder;
	}
	
	
	
	private void loadSnackItem(final VendingMachine vendingMachine) {
		uiVBSnackItemPanel.removeAll();
		
		
		for(String name : vendingMachine.getSnackItemNames()) {
			List<SnackItem> snackItems =vendingMachine.getSnackItemsByName(name);
			if(snackItems.isEmpty()) {
				UIVBSnackItem uiVbSnackItem  = new UIVBSnackItem(null, null);
					uiVBSnackItemPanel.add(uiVbSnackItem);
			
			} else {
				final SnackItem snackItem = snackItems.get(0);
				
				
				final UIVBSnackItem uiVbSnackItem = new UIVBSnackItem(snackItem,snackItems.size());
				
				
				
				
				uiVbSnackItem.getBuyButton().addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						buySnackItem(vendingMachine, snackItem);
						
					}
				});
				uiVBSnackItemPanel.add(uiVbSnackItem);
			}
			
			
			
		}
		repaint();
		revalidate();
		

	}

	
	private void buySnackItem(VendingMachine vendingMachine,  SnackItem snackItem) {
		String snackName = snackItem.getName();
		try {
			if(cardHolder != null) {
				cardHolder.buyItem(snackItem);
				vendingMachine.buySnackItem(snackName);
				
				UIHelper.showConfirmMessage(this, snackItem.getName() + " dispensed from Vending Machine. \n");
			} else {
				showPayCashDialog(vendingMachine, snackItem);
			}
			
		} catch (VendingMachineException e) {
			
			e.printStackTrace();
		} catch(CaloriesExceededException cee) {
			UIHelper.showErrorMessage(this, "Calories Exceeded!!");
			
		} catch(InsufficientFundsException iee) {
			UIHelper.showErrorMessage(this, "Insufficient Fund!!");
		} catch(Exception e) {
			e.printStackTrace();
		}
		
	}


	@Override
	public void onUpdate(VendingMachine vendingMachine) {
		loadSnackItem(vendingMachine);
		
	}



	
	
	
	
	
	
	
	private void showPayCashDialog(final VendingMachine vendingMachine, SnackItem snackItem) {
		int response = JOptionPane.showConfirmDialog(null,
				"You are not Card Holder Member. Would you like to pay in Cash?\n" 
				+ "Total is " + UIHelper.currencyFormat.format(snackItem.getPrice()),
				"Confirm", JOptionPane.YES_NO_OPTION,
				JOptionPane.QUESTION_MESSAGE);
		if (response == JOptionPane.NO_OPTION) {
			
		} else if (response == JOptionPane.YES_OPTION) {
			try {
				vendingMachine.buySnackItem(snackItem.getName());
				UIHelper.showConfirmMessage(this, snackItem.getName() + " dispensed from Vending Machine. \n");
			} catch (VendingMachineException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
	
	

}
